﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    /// <summary>
    /// 请销假管理
    /// </summary>
    public class ApplyController : BaseController
    {
        private readonly IApplyService _applyService;

        public ApplyController(IApplyService applyService)
        {
            _applyService = applyService;
        }
        // GET: Apply
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult List4Stu()
        {
            return View();
        }

        public ActionResult Detail4Stu(long id)
        {
            ViewBag.Id = id;
            return View();
        }

        public ActionResult List4Teacher()
        {
            return View();
        }

        public ActionResult Detail4Teacher(long id)
        {
            ViewBag.Id = id;
            return View();
        }


        public ActionResult GetApplyListByApplyUserId()
        {
            var result = _applyService.GetApplyListByApplyUserId(CurrentUser.UserId);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetApplyList()
        {
            var result = _applyService.GetApplyList();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetApplyById(long id)
        {
            var result = _applyService.GetApplyListByApplyId(id);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(ApplyEntity applyVO)
        {
            try
            {
                if (applyVO.Title.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入标题！");
                }
                if (applyVO.ApplyStartTime==null)
                {
                    throw new Exception("请选择开始时间！");
                }
                if (applyVO.ApplyEndTime == null)
                {
                    throw new Exception("请选择结束时间！");
                }
                if (applyVO.ApplyRemark.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入请假内容！");
                }
                applyVO.ApplyTime = DateTime.Now;
                applyVO.ApplyUserId = CurrentUser.UserId;
                applyVO.Status = 2;
                applyVO.StatusTime = DateTime.Now;
                var result = _applyService.Add(applyVO);
                return Content(result.Id.ToString());
            }
            catch (Exception ex)
            {
                return Content("err:" + ex.Message);
            }

        }

        [HttpPost]
        public ActionResult Approve(long id, string approveRemark, int approveStatus)
        {
            var applyEntity = _applyService.Find(id);
            if (applyEntity != null)
            {
                applyEntity.ApproveRemark = approveRemark;
                applyEntity.Status = approveStatus;
                applyEntity.ApproveUserId = CurrentUser.UserId;
                _applyService.Update(applyEntity);
            }
            return Content("true");
        }
    }
}